#include "gui.h"
#include "../logic/logic.h"
//-----------Cell-------------------
/*Function: Cell constructor
  Semantics: creates dummy cell*/
Cell :: Cell() : Building ()
{
    isActive = false;
    x = y = 0;
    //printf("cell construct: %s, %d\n", getName(), getPrice());
};

/*Function: Cell constructor ()
  Semantics: creates certain cell*/
Cell :: Cell( char * n, int pr, int inc = 100)
 : Building ( n, pr, inc)
{
    //name = new char [sizeof(n)+1];
    //strcpy ( name, n);
    //price = pr;
    isActive = false;
    x = y = 0;
};

void Cell ::  operator = (Cell &cc)
{
    setName(cc.getName());
    setPrice(cc.getPrice());
    y = cc.y;
    x = cc.x;
    isActive = cc.isActive;
}

/*Function: Cell destructor ()
  Semantics: delete cell*/
Cell :: ~Cell()
{
};

/*Function: drawCell ()
  Semantics: print cell's content to grid g, with painter*/
void Cell :: drawCell ( Grid *g, QPainter *painter)
{    
    static int border = DefaultActiveBorder;
    static int height = g->y_size;
    static int width = g->x_size;
    static int xPos, yPos;
    xPos = x * width;
    yPos = y * height;
    
    QBrush  lastBrush = painter->brush();
    QPen  lastPen = painter->pen();

    //all data printed in this rect
    QRectF doubleRect;
    QRectF rect ( 
        xPos + border, 
        yPos + border, 
        width-2*border, 
        height - 2*border);
    // fill field
    QRadialGradient gradient(
        xPos + width/2, yPos + height/2, 
        qMax ( width, height),
        xPos + width/2, yPos + height/2);
    gradient.setColorAt(0, innerColor());
    gradient.setColorAt(1, outerColor());
    painter->setBrush(gradient);
    painter->drawRoundedRect ( rect, 7.0, 4.5);
    // load previous brush 
    painter->setBrush(lastBrush);
    painter->setPen(lastPen);

    if ( isActive){
        doubleRect = QRectF ( xPos + 2*border, yPos + 2*border, 
        width - 4*border, height - 4*border);
        painter->drawRoundedRect ( doubleRect, 7.0, 4.5);
    }
    if ( getOwner() != NoPlayer ){
        doubleRect = QRectF ( 
            xPos + border*2 + EllPos,
            yPos + border*2 + EllPos, 
            EllSize, EllSize);
        lastBrush = painter->brush();
        painter->setBrush( g->playerColor[getOwner()] );
        painter->drawEllipse(doubleRect);
        painter->setBrush(lastBrush);
    }

    sprintf ( tmp, "%s\n%s\n(%d %d)$", getName(),getType()->typeName, getPrice(), getIncome());
    painter->drawText ( rect, Qt::AlignCenter, tr(tmp));
};